/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.datastructure;

import java.util.HashMap;

/**
 * @author Massimo Canonaco
 *
 */
public class ConstantSizeMap<K, V> extends HashMap<K, V> {
	
	/** */
	private static final int DEFAULT_SIZE = 8;
	private static final long serialVersionUID = 1010216166067777565L;
	
/** */
private final int MAX_SIZE;
	
	/** */
	public ConstantSizeMap(){
		super(DEFAULT_SIZE);
		MAX_SIZE = DEFAULT_SIZE;
	}
	
	/** */
	public ConstantSizeMap(int max_capacity){
		super(max_capacity);
		MAX_SIZE = max_capacity;
	}
	
	/** */
	public ConstantSizeMap(int max_capacity, float load_factor){
		super(max_capacity, load_factor);
		MAX_SIZE = max_capacity;
	}
	
	/** */
	@Override
	public V put(K element_key, V element_value){
		V previous_value = null;
		if(super.size() < MAX_SIZE){
			previous_value = super.put(element_key, element_value);
		}else if((previous_value = super.get(element_key)) != null) // if it's already present
			super.put(element_key, element_value);					// overwrite the old value
		
		return previous_value;
	}
}
